#include "percal.h"

Time::Time(int hour,int minu,int sec)
        :_hour(hour)
        ,_minu(minu)
        ,_sec(sec)
{}
Time::Time(const Time& t)
        :_hour(t._hour)
        ,_minu(t._minu)
        ,_sec(t._sec)
{}

Calender:: Calender(int year, int month,int day,Time t)
    :_month(month)
    , _day(day)
    ,_year(year)
    ,_start((DaySum(year) % 7 + 4) % 7)
    ,_t(t)
{}

Calender::Calender(const Calender& cal)
    :_month(cal._month)
    ,_day(cal._day)
    ,_year(cal._year)
    ,_start(cal._start)
    ,_t(cal._t)
{}

int& Calender::GetStart(void)
{
    return _start;
}

int Calender::GetYear(void)
{
    return _year;
}

static inline int MonthDays(int year,int month)
{
    static int DayArr[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
    int day=DayArr[month];
    if(month==2&&((year%4==0&&year%100!=0)||year%400==0))
    {
        day=29;
    }
    return day;
}
void PrintMonth(int month)
{
    static const char* mon[]={"January","February","March","April","May","June","July","August","September","October","November","December"};
    cout<<"              "<<mon[month-1]<<endl;
}

void split(void)
{
    cout<<endl;
    cout<<"-------------------------------------------"<<endl;
}



static int YearDay(int year)
{

    if((year%4==0&&year%100!=0)||year%400==0)
        return 366;
    return 365;
}



int DaySum(int year)
{
    int total=0;
    for(int i=1970;i<year;i++)
    {
        total+=YearDay(i);
    }
    return total;
}


void Calender::Print_Calender()
{
    int i;
    for (i = 1; i <= 12; i++)
    {
        PrintMonth(i);
        split();
        PrintWeek();

        PrintDate(*this, i);
        split();
        cout << endl;
    }
}

void PrintDate(Calender& y,int month)
{
    int year = y.GetYear();
    y.GetStart()= (y.GetStart() + MonthDays(year, month - 1) % 7) % 7;
    int Monthday = MonthDays(year, month);
    int tmp = y.GetStart();
    int start = y.GetStart();
    for (int i = 1; i <= Monthday; i++)
    {
        while (tmp)
        {
            cout << "      ";
            tmp--;
        }
        cout << setw(3) << i;
        if ((start + i) % 7 == 0)
            cout << endl;
        else
            cout << "   ";

    }
}

void Calender::GetTime(void)
{
    time_t t=time(0);
    int day=t/(24*3600);
    int sec=t%(24*3600);
    int year=1970;
    int month=1;

    while(day>365||day>366)
    {
        day-=YearDay(year);
        year++;
    }
    _year=year;
    while(day>MonthDays(year,month))
    {
        day-=MonthDays(year,month);
        month++;
    }
    _month=month;
    _day=day+1;

    _t.Second()=sec%60;
    _t.Hour()=sec/3600+8;
    _t.Minute()=(sec%3600)/60;
}

void Probar(void)
{
    char bar[102];
    memset(bar,'\0',sizeof(bar));
    int i=0;
    char ch[]={'|','/','-','\\'};
    while(i<=100)
    {
        bar[i]='#';
        printf("[%-100s]-%d%%-[%c]\r",bar,i,ch[i%4]);
        fflush(stdout);
        i++;
        usleep(30000);
    }
    printf("\n>>加载成功<<\n");
    
}




void Calender::Print_Month(void)
{
    cout<<_year<<"-"<<_month<<endl;
    PrintMonth(_month);
    split();
    for(int i=1;i<_month;i++)
    {
        _start= (_start + MonthDays(_year,i - 1) % 7) % 7;
    }

    PrintWeek();
    PrintDate(*this,_month);
    split();

}




void PrintWeek(void)
{
    cout<<"Sun   Mon   Tue   Wed   Thu   Fri   Sat"<<endl;
}
